// 气源系统 10662 返回前端部署数据
dev.action = function (resp, param) {

    if (param.spaceId == undefined) {
        resp.setJsData("请传参数spaceId");
        return;
    }

    if (param.id == undefined) {
        resp.setJsData("请传参数设备ID");
        return;
    }

    //图片ID
    //图片ID
    var PicMap = {
        '离心机': '1001',
        '螺杆机': '1002',
        '吸干机': '1003',
        '冷却塔': '1004',
        '冷却泵': '1005',
        '冷冻泵': '1006',
        '储气罐': '1007',
        '冷干机': '1008',
        '控制阀': '1010',
        '干燥机': '1011',
        '流量计': '1012',
        '温度计': '1013',
        '其他': '1999',
        '制冷主机': '1009'
    }



    var deviceType = iot.ctx("iotDevServiceImpl").getStationSpaceDevices(param.spaceId);

    // 处理是否有数据
    if (deviceType['储气罐'] == null) {
        deviceType['储气罐'] = 0
    }
    if (deviceType['冷干机'] == null) {
        deviceType['冷干机'] = 0
    }
    if (deviceType['吸干机'] == null) {
        deviceType['吸干机'] = 0
    }
    if (deviceType['离心机'] == null) {
        deviceType['离心机'] = 0
    }

    if (deviceType['螺杆机'] == null) {
        deviceType['螺杆机'] = 0
    }

    deviceType['空压机'] = deviceType['螺杆机'] + deviceType['离心机'];

    //resp.setJsData(deviceType);
    //return;

    var result = {};
    // 请求实时属性的设备ID
    result.deviceId = 10662;
    var checkSql = "SELECT id,name FROM `iot_virtual_item` where dev_id = 10662";
    var dataTypeIds = iot.db().qry(checkSql);
    var deviceData = {};
    //var deviceType = {'空压机': 5, '储气罐': 2, '冷干机': 5, '吸干机': 2};

    for (var key in deviceType) {

        if (key == '离心机' || key == '螺杆机') {
            continue;
        }

        if (key == '空压机') {
            var hosts = iot.list();
            for (var i = 1; i < deviceType[key] + 1; i++) {
                var curHost = {};
                var curCtrl = {};
                if (i < deviceType['螺杆机'] + 1) {
                    curHost.name = i + '#螺杆机';
                    curCtrl.name = i + '#螺杆机出气控制阀';
                    curCtrl.pic = PicMap['控制阀'];
                    curHost.pic = PicMap['螺杆机']
                } else {
                    curHost.name = (i-3) + '#离心机';
                    curCtrl.name = (i-3) + '#离心机出气控制阀';
                    curCtrl.pic = PicMap['控制阀'];
                    curHost.pic = PicMap['离心机']
                }
                curHost.index = i;
                curHost.status = 0;
                curCtrl.index = i;
                curCtrl.status = 0;
                curHost.left = curCtrl;
                curHost.right = {};
                dataTypeIds.forAll(function (item) {
                    if (item.name.indexOf(curCtrl.name) > -1) {
                        curCtrl.id = item.id;
                    }
                })
                dataTypeIds.forAll(function (item) {
                    if (item.name.indexOf(curHost.name) > -1) {
                        curHost.id = item.id;
                    }
                })
                hosts.add(curHost);
            }
            deviceData['空压机'] = {
                hosts: hosts
            }
        } else {
            var hosts = iot.list();
            for (var i = 1; i < deviceType[key] + 1; i++) {
                var curHost = {};
                var curCtrl = {};
                curHost.name = i + '#' + key;
                curHost.index = i;
                curHost.pic = PicMap[key]
                curHost.status = 0;
                if (key === '冷干机' || key === '吸干机') {
                    curCtrl.name = i + '#' + key +'进气控制阀';
                    curCtrl.index = i;
                    curCtrl.status = 0;
                    curCtrl.pic = PicMap['控制阀'];
                    dataTypeIds.forAll(function (item) {
                        if (item.name.indexOf(curCtrl.name) > -1) {
                            curCtrl.id = item.id;
                        }
                    })
                }
                curHost.left = curCtrl;
                curHost.right = {};
                dataTypeIds.forAll(function (item) {
                    if (item.name.indexOf(curHost.name) > -1) {
                        curHost.id = item.id;
                    }
                })
                hosts.add(curHost);
            };
            deviceData[key] = {
                hosts: hosts
            }
        }
    }
    // 把设备数据塞进result
    result['devices'] = deviceData;
    resp.setJsData(result);
}
